package pe.edu.upc.e41a.javasoft.domain;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@Entity
@Table(name = "publicacion")
/*
@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "publicacion")
@RooDbManaged(automaticallyDelete = true)
*/
public class Publicacion {

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "codigo", columnDefinition = "INT")
    private Integer codigo;

	public Integer getCodigo() {
        return this.codigo;
    }

	public void setCodigo(Integer id) {
        this.codigo = id;
    }

	@OneToMany(mappedBy = "publicacion")
    private Set<Imagen> imagens;

	@OneToMany(mappedBy = "publicacion")
    private Set<Transaccion> transaccions;

	@OneToMany(mappedBy = "publicacion")
    private Set<Visita> visitas;

	@ManyToOne
    @JoinColumn(name = "categoria", referencedColumnName = "codigo", nullable = false)
    private Categoria categoria;

	@ManyToOne
    @JoinColumn(name = "usuario", referencedColumnName = "codigo", nullable = false)
    private Usuario usuario;

	@Column(name = "titulo", columnDefinition = "VARCHAR", length = 100)
    @NotNull
    private String titulo;

	@Column(name = "descripcion", columnDefinition = "TEXT")
    private String descripcion;

	@Column(name = "tipo", columnDefinition = "CHAR")
    @NotNull
    private Character tipo;

	@Column(name = "cantidad", columnDefinition = "INT")
    @NotNull
    private Integer cantidad;

	@Column(name = "precio", columnDefinition = "DECIMAL", precision = 10, scale = 2)
    @NotNull
    private BigDecimal precio;

	@Column(name = "fecha_creacion", columnDefinition = "DATETIME")
    @NotNull
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date fechaCreacion;

	@Column(name = "duracion", columnDefinition = "INT")
    @NotNull
    private Integer duracion;

	@Column(name = "estado", columnDefinition = "CHAR")
    @NotNull
    private Character estado;
	
	public static final Character ESTADO_ACTIVO = 'A';
	public static final Character ESTADO_CADUCADO = 'C';
	public static final Character ESTADO_VENDIDO = 'V';

	public Set<Imagen> getImagens() {
        return imagens;
    }

	public void setImagens(Set<Imagen> imagens) {
        this.imagens = imagens;
    }

	public Set<Transaccion> getTransaccions() {
        return transaccions;
    }

	public void setTransaccions(Set<Transaccion> transaccions) {
        this.transaccions = transaccions;
    }

	public Set<Visita> getVisitas() {
        return visitas;
    }

	public void setVisitas(Set<Visita> visitas) {
        this.visitas = visitas;
    }

	public Categoria getCategoria() {
        return categoria;
    }

	public void setCategoria(Categoria categoria) {
        this.categoria = categoria;
    }

	public Usuario getUsuario() {
        return usuario;
    }

	public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }

	public String getTitulo() {
        return titulo;
    }

	public void setTitulo(String titulo) {
        this.titulo = titulo;
    }

	public String getDescripcion() {
        return descripcion;
    }

	public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

	public Character getTipo() {
        return tipo;
    }

	public void setTipo(Character tipo) {
        this.tipo = tipo;
    }

	public Integer getCantidad() {
        return cantidad;
    }

	public void setCantidad(Integer cantidad) {
        this.cantidad = cantidad;
    }

	public BigDecimal getPrecio() {
        return precio;
    }

	public void setPrecio(BigDecimal precio) {
        this.precio = precio;
    }

	public Date getFechaCreacion() {
        return fechaCreacion;
    }

	public void setFechaCreacion(Date fechaCreacion) {
        this.fechaCreacion = fechaCreacion;
    }

	public Integer getDuracion() {
        return duracion;
    }

	public void setDuracion(Integer duracion) {
        this.duracion = duracion;
    }

	public Character getEstado() {
        return estado;
    }

	public void setEstado(Character estado) {
        this.estado = estado;
    }

	/*
	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Publicacion().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countPublicacions() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Publicacion o", Long.class).getSingleResult();
    }

	public static List<Publicacion> findAllPublicacions() {
        return entityManager().createQuery("SELECT o FROM Publicacion o", Publicacion.class).getResultList();
    }

	public static Publicacion findPublicacion(Integer codigo) {
        if (codigo == null) return null;
        return entityManager().find(Publicacion.class, codigo);
    }

	public static List<Publicacion> findPublicacionEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Publicacion o", Publicacion.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Publicacion attached = Publicacion.findPublicacion(this.codigo);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Publicacion merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Publicacion merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }
    */
}
